<?php

namespace App\Http\Controllers\Admin\Auth;

use App\Http\Common\Constant;
use App\Http\Models\Admin\AdminUsers;
use App\Http\Models\Admin\Auth\Role;
use App\Http\Models\Admin\Auth\UserRole;
use App\Http\Responses\ResponseDto;
use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\View;
use Psy\Util\Json;

class AdminUsersController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        //
        //
        return View::make('admin.auth.adminusers')->withUserinfo($request->session()->get('adminUser'))
            ->withUrltitle(trans("admin/message.adminusers_index"))
            ->withAssets($request['ASSETS']);
    }
    public function page(Request $request)
    {
        $datas = $request->input();
        $start = $datas[3]['value'];
        $length = $datas[4]['value'];

        $users = DB::table('admin_users')->where('is_admin',"<>",1)->skip($start)->take($length)->get();
        $total = DB::table('admin_users')->count();
        return ResponseDto::createResObj(ResponseDto::convertPageToDataTableObj($users,$total),ResponseDto::$SUCCESS);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create(Request $request)
    {
        $user_id = $request->input('id');
        $ids = DB::table('user_role')->select('roleid')->where('userid',$user_id)->get();


        $array = array_pluck($ids, 'roleid');
        $list = Role::all();
        foreach($list as $data){
            $data->name = $data->role_name;
            if($this->validateResource($data,$array)){
                $data->checked = true;
            }
        }


        return Json::encode($list);
    }



    public function validateResource($data,$values)
    {
        if($values == null){
            return false;
        }
        foreach($values as $id){
            if($data->id == $id){
                return true;
            }
        }
        return false;

    }

    /**
     * 保存用户的角色信息
     * @param Request $request
     * @return string
     */
    public function saveUserRole(Request $request)
    {
        //
        $user_id = $request->input("user_id");
        $noids = $request->input("noids");
        $res_ids = array();
        if($noids != null && $noids != ""){
            $res_ids = explode(",",$noids);
        }

        UserRole::where('userid',$user_id)->delete();
        foreach($res_ids as $roid){
            $ur = new UserRole();
            $ur->userid = $user_id;
            $ur->roleid = $roid;
            $ur->save();
        }
        return ResponseDto::createResObj(trans("admin/message.update_success"), ResponseDto::$SUCCESS);
    }

    public function resetpwd(Request $request){
        $id = $request->input('id');
        if($id){
            $aduser = AdminUsers::find($id);
            if($aduser){
                $npwd = Constant::getRandChar(8);
                $aduser->password = md5($npwd);
                $aduser->save();
                return ResponseDto::createResObj($npwd, ResponseDto::$SUCCESS);
            }
        }
        return ResponseDto::createResObj(trans("admin/message.update_fail"), ResponseDto::$FAIL);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
        if ($request->input("id")) {
            $validator = Validator::make(Input::all(),
                [
                    'user_name' => 'required',

                ]);
        }else{
            $validator = Validator::make(Input::all(),
                [
                    'user_name' => 'required',
                    'password' => 'required',
                ]);
        }
        if ($validator->fails()) {
            return ResponseDto::createResObj($validator->errors()->first(), ResponseDto::$FAIL);
        } else {
            $aduser = new AdminUsers();
            $aduser->user_name = $request->input("user_name");
            if ($request->input("nick_name") != null) {
                $aduser->nick_name = $request->input("nick_name");
            } else {
                $aduser->nick_name = $request->input("user_name");
            }
            $aduser->email = $request->input("email");
            $aduser->photo_url = $request->input("photo_url");


            if ($request->input("id")) {
                //更新操作
                $exit_item = AdminUsers::where('id', '<>', $request->input("id"))->where('user_name', $aduser->user_name)->first();
                if ($exit_item) {
                    return ResponseDto::createResObj(trans("admin/message.name_exit"), ResponseDto::$FAIL);
                } else {
                    $up_item = AdminUsers::find($request->input("id"));
                    $up_item->user_name = $aduser->user_name;
                    $up_item->nick_name = $aduser->nick_name;
                    $up_item->email = $aduser->email;
                    $up_item->photo_url = $aduser->photo_url;
                    if ($up_item->save()) {
                        return ResponseDto::createResObj(trans("admin/message.update_success"), ResponseDto::$SUCCESS);
                    } else {
                        return ResponseDto::createResObj(trans("admin/message.service_fail"), ResponseDto::$FAIL);
                    }
                }
            } else {
                //添加操作
                $find = AdminUsers::where('user_name', $request->input("user_name"))->first();
                if ($find) {
                    return ResponseDto::createResObj(trans("admin/message.name_exit"), ResponseDto::$FAIL);
                }
                $aduser->password = md5($request->input("password"));
                if ($aduser->save()) {
                    return ResponseDto::createResObj(trans("admin/message.submit_success"), ResponseDto::$SUCCESS);
                } else {
                    return ResponseDto::createResObj(trans("admin/message.service_fail"), ResponseDto::$FAIL);
                }
            }


        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
        return Json::encode(AdminUsers::find($id));
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        AdminUsers::destroy($id);
//        PostTags::where('tag_id',$id)->delete();
        return ResponseDto::createResObj(trans("admin/message.update_success"),ResponseDto::$SUCCESS);
    }
}
